package com.qdu.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qdu.entity.Housetype;
import com.qdu.entity.Users;
import com.qdu.service.HomemessageService;
import com.qdu.service.HousetypeService;

@Controller
@RequestMapping("/housetype")
public class HousetypeController {

    @Autowired
    private HousetypeService housetypeService;
    
    @Autowired
    private HomemessageService homemessage;
    
    @GetMapping("/publichouse_page/{Uid}")
    public String publishhouse_page(@PathVariable String Uid,Model model) {
    	model.addAttribute("Uid",Uid);
    	return "publishhouse_page";
    	
    }
	
    @PostMapping("/publishhouse")
    public String publishhouse(Housetype House,Model model,HttpSession session) {
    	House.setHatt("1");
    	housetypeService.addHouse(House);
    	
		session.setAttribute("houselist",housetypeService.getallHouseList());
    	return "index";
    }
    
    @GetMapping("/houselistbyuid/{Uid}")
	public String housebyuid(@PathVariable String Uid,Model model) {
    	model.addAttribute("houselistbyuid",housetypeService.getHouseByuid(Uid));
    	return "houselistbyuid";
    }
    
    @GetMapping("/housetypeinfo/{Uid}/{Hid}")
    public String houseinfo(@PathVariable String Uid,@PathVariable String Hid,Model model) {
    	model.addAttribute("house",housetypeService.gethousebyuidandhid(Uid, Hid));
    	model.addAttribute("comment",homemessage.getmessageById(Uid, Hid));
    	return "houseInfo";
    }
    @GetMapping("/housetypeinfoother/{Uid}/{Hid}")
    public String houseinfoother(@PathVariable String Uid,@PathVariable String Hid,Model model,HttpSession session) {
    	model.addAttribute("house",housetypeService.gethousebyuidandhid(Uid, Hid));
    	Users user = (Users)session.getAttribute("sessionuser");
    	model.addAttribute("comment",homemessage.getmessageById(Uid, Hid));
    	return "houseInfoother";
    }
    
    @PostMapping("/edit_house")
    public String edituser(Housetype house,Model model,HttpSession session) {
    	Users user=(Users)session.getAttribute("sessionuser");
    	if(user.getUid().equals(house.getUid())){
    		housetypeService.updateHouse(house);
    		return "index";
    	}
    	else {
    		return "powerless";
    	}
    }
    
    @PostMapping("/findbyHtopic")
    public String findbyHad(String Htopickey,Model model) {
    	model.addAttribute("findbyHtopic",housetypeService.getHouseBytopic(Htopickey));
    	return "findbyHtopic_page";
    }
    
    @GetMapping("/findrenthouse")
    public String findbyHatt(Model model) {
    	model.addAttribute("findbyHatt",housetypeService.getHouseByatt("3"));
    	return "houserentlist";
    }
    
    @GetMapping("/findbyHprice")
    public String findbyHprice(String high,String low,Model model) {
    	model.addAttribute("houselistbyHprice",housetypeService.getHouseByprice(high, low));
    	return "houselistbyprice";
    }
    
    
    
} 

